package com.zc.im.controller;


import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zc.im.model.im.group.ZcImGroupMarkScore;
import com.zc.im.service.im.group.ImGroupMarkScoreService;
import com.zc.im.util.JsonUtils;
import com.zc.im.util.ResponseJson;
import com.zc.im.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.Map;

/**
 * 打分controller
 * Created by wangcc on 2017/7/8.
 */
@Controller
@RequestMapping(value = "/groupmarkscore")
public class ImGroupMarkScoreController {

    @Autowired
    private ImGroupMarkScoreService imGroupMarkScoreService;
    private Logger logger = Logger.getLogger(ImGroupMarkScoreController.class);


    /**
     * 添加打分
     *   打分发送的json{"groupId":"2" ,"logId":"2","score":"80%","markUserId":"12028" ,"companyId" : "1212"}
     * @param groupmarkscore
     * @return
     */
    @RequestMapping(value = "/insertgroupmarkscore",method = RequestMethod.POST)
    public ResponseJson  insertGroupMarkScore(String groupmarkscore) {
        ResponseJson responseJson = new ResponseJson();
        if (StringUtils.isEmpty(groupmarkscore)) {
            return responseJson;
        }
        ZcImGroupMarkScore zcImGroupMarkScoreModel = JsonUtils.strToClass(groupmarkscore, ZcImGroupMarkScore.class);
        Integer key = null;
        try {
            key= imGroupMarkScoreService.saveGroupMarkScore( zcImGroupMarkScoreModel );
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("key",key);
        responseJson.setDataMap(dataMap);
        return responseJson;
    }

    /**
     * 查询日志的分数
     * 发送过来的json("log_id":"要查询的日志id")
     * @param findGroupMarkScore
     * @return
     */
    @RequestMapping(value = "/findGroupMarkScore",method = RequestMethod.POST)
    public ResponseJson findGroupMarkScore(String findGroupMarkScore) {
        ResponseJson responseJson = new ResponseJson();
        if (StringUtils.isEmpty(findGroupMarkScore)) {
            return  responseJson;
        }
        JSONObject jsonType = JsonUtils.strToJsonObj(findGroupMarkScore);
        ZcImGroupMarkScore zcImGroupMarkScore = null;
        zcImGroupMarkScore = imGroupMarkScoreService.queryGroupMarkScore(Integer.parseInt(jsonType.getString("logId")));
        Map<String ,Object > dateMap = new HashMap<>();
        dateMap.put("key",JsonUtils.objToStr(zcImGroupMarkScore));
        responseJson.setDataMap(dateMap);
        return responseJson;

    }

}
